package cn.lunkr.sdkdemo.ui.table;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;

import com.facebook.drawee.generic.RoundingParams;
import com.facebook.drawee.view.SimpleDraweeView;
import com.google.android.material.navigation.NavigationBarView;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.core.content.res.ResourcesCompat;
import androidx.fragment.app.Fragment;
import androidx.navigation.NavController;
import androidx.navigation.NavGraph;
import androidx.navigation.NavigatorProvider;
import androidx.navigation.fragment.NavHostFragment;
import androidx.navigation.ui.NavigationUI;

import cn.lunkr.base.CApp;
import cn.lunkr.base.config.SizeConfig;
import cn.lunkr.base.entity.PictureOptions;
import cn.lunkr.base.entity.PictureOptionsOther;
import cn.lunkr.base.mail.util.MailUtil;
import cn.lunkr.base.mgr.MeManager;
import cn.lunkr.base.net.MailNetConfig;
import cn.lunkr.base.service.InitService;
import cn.lunkr.base.ui.BasicActivity;
import cn.lunkr.base.util.SystemUtil;
import cn.lunkr.base.util.image.PictureLoader;
import cn.lunkr.sdkdemo.R;
import cn.lunkr.sdkdemo.databinding.ActivityCmMainTableBinding;
import cn.lunkr.sdkdemo.ui.table.fragment.TableEmailFragment;
import cn.lunkr.sdkdemo.util.FixFragmentNavigator;

public class CMMainLandActivity extends BasicActivity {

    ActivityCmMainTableBinding mViewBinding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mViewBinding = ActivityCmMainTableBinding.inflate(getLayoutInflater());
        setContentView(mViewBinding.getRoot());
        initView();
        startInitServie(InitService.OAB_CALLBACK);
    }

    private void initView() {
        NavHostFragment navHostFragment =
                (NavHostFragment) getSupportFragmentManager()
                        .findFragmentById(R.id.nav_host_fragment);
        if (navHostFragment != null) {
            NavController navController = navHostFragment.getNavController();
            NavigationUI.setupWithNavController(mViewBinding.navigationRailView, navController);
            setFixNavigator(navController, navHostFragment);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        SimpleDraweeView avatarView = mViewBinding.navigationRailView.findViewById(R.id.my_navigation_rail_fab);
        avatarView.setVisibility(View.VISIBLE);
        PictureLoader.getInstance().load(avatarView,
                new PictureOptions.Builder().setDrawable(ResourcesCompat.getDrawable(getResources(), R.drawable.vd_abc_portrait_default, null))
                        .setUrl(MailNetConfig.getPortraitUrl(MailUtil.parseEmailFromAddress(MeManager.getPrimaryEmail()), null, false)).build(),
                new PictureOptionsOther.Builder().setRoundingParams(RoundingParams.fromCornersRadius(SizeConfig.dp2px(getApplicationContext(), 2))).build());
    }

    private void startInitServie(int type) {
        final Context C = CApp.get();
        Intent intent = new Intent(C, InitService.class);
//        intent.setPackage(C.getPackageName());
//        intent.setAction(Actions.ACTION_INIT_SERVICE);
        intent.putExtra("force_update", true);
        intent.putExtra("type", type);
        SystemUtil.compatStartService(C, intent);
    }

    private void setFixNavigator(NavController navController, Fragment fragmentById) {
        NavigatorProvider navigatorProvider = navController.getNavigatorProvider();
        FixFragmentNavigator fixFragmentNavigator =
                new FixFragmentNavigator(this, fragmentById.getChildFragmentManager(), fragmentById.getId());
        navigatorProvider.addNavigator(fixFragmentNavigator);
        NavGraph navDestinations = new InitTableNavGraph().init(CMMainLandActivity.this, navigatorProvider, fixFragmentNavigator);
        navController.setGraph(navDestinations);
        mViewBinding.navigationRailView.setOnItemSelectedListener(new NavigationBarView.OnItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                navController.navigate(item.getItemId());
                return true;
            }
        });
    }

    @Override
    public void onBackPressed() {
        if (mViewBinding.navigationRailView.getSelectedItemId() == R.id.tab_mails) {
            Fragment fragment = getFragment(TableEmailFragment.class);
            if (fragment != null && (fragment instanceof TableEmailFragment)
                    && ((TableEmailFragment) fragment).onBackPressed()) {
                return;
            }
        }
        moveTaskToBack(true);
    }

    public Fragment getFragment(Class<?> clazz) {
        List<Fragment> fragments = getSupportFragmentManager().getFragments();
        if (fragments != null && fragments.size() > 0) {
            NavHostFragment navHostFragment = (NavHostFragment) fragments.get(0);
            List<Fragment> childFragments = navHostFragment.getChildFragmentManager().getFragments();
            if (childFragments != null && childFragments.size() > 0) {
                for (int j = 0; j < childFragments.size(); j++) {
                    Fragment fragment = childFragments.get(j);
                    if (fragment.getClass().isAssignableFrom(clazz)) {
                        return fragment;
                    }
                }
            }
        }
        return null;
    }

}